var _set = require("../../../API/set.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number:100,
    price:0,
    total:0,
    mark:'',
    name:'',
    phone:'',
    address:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self =this;
    wx.getSystemInfo({
      success: function(t){
        // console.log(t)
        self.setData({
            systemInfo:t,
        })
        }
    });
  },
  changeNumber:function(e){
    this.setData({
      number:e.detail,
      total:(e.detail * parseFloat(this.data.info.price)).toFixed(2)
    })
  },
  nameChange:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  phoneChange:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  addrChange:function(e){
    this.setData({
      address:e.detail.value
    })
  },
  getInfo:function(){
    var self = this;
    (0, _set.getTicketInfo)().then(function(t) {
        self.setData({
          info: t.data,
          total:(self.data.number * parseFloat(t.data.price)).toFixed(2)
        });
        wx.setNavigationBarTitle({ title: t.data.top_title})
    }).catch(function(t) {
      return app.Tips({
          title: t
      });
  });
  },
  bindTextAreaBlur:function(e){
    this.setData({
      mark:e.detail.value
    })
  },
  payOrder:function(e){
    if(!this.data.name&&this.data.name == ''){
      return app.Tips({
        title: '请输入收货人姓名'
      })
    };
    if(!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.data.phone)){
      return app.Tips({
        title: '请输入手机号'
      })
    };
    if(!this.data.address&&this.data.address == ''){
      return app.Tips({
        title: '请输入收货人姓名'
      })
    };
    void wx.showLoading({
      title: "支付中..."
    }), (0, _set.ticketPay)({
      type:3,
      pay_type:'weixin',
      price:this.data.info.price,
      num:this.data.number,
      remark:this.data.mark,
      to_name:this.data.name,
      to_address:this.data.address,
      to_phone:this.data.phone
    }).then(function(t) {
      wx.hideLoading();
      var e = t.data.status, i = t.data.result.data, r = "/pages/order/order?type=9"; 
      if(t.data.pay_type == "yue"){
          if(e == "SUCCESS"){
                  return wx.hideLoading(),app.Tips({
                      title: t.msg,
                      icon: "success"
                  }, {
                      tab: 5,
                      url: r
                  })
          }else{
              return wx.hideLoading(), app.Tips({
                  title: t.data.message
              }, {
                  tab: 5,
                  url: ""
              });
          }
      }else if(t.data.pay_type == "weixin_pay"){
           wx.requestPayment({
                timeStamp: i.timestamp,
                nonceStr: i.nonceStr,
                package: i.package,
                signType: i.signType,
                paySign: i.paySign,
                success: function(res) {
                    return wx.hideLoading(),app.Tips({
                        title: "支付成功",
                        icon: "success"
                    }, {
                        tab: 1,
                        url: r
                    })
                },
                fail: function(res) {
                  console.log(res)
                  return wx.hideLoading()
                },
            });
      }else{
          if(t.data.status == "SUCCESS"){
              app.Tips({
                  title: t.msg,
                  icon: "success"
              }, {
                  tab: 1,
                  url: r
              })
          }else{
              app.Tips({
                  title: t.msg,
                  icon: "none"
              })
          }
      }
    }).catch(function(t) {
      return app.Tips({
          title: t
      });
  });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})